/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("dj_stpo",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using DjErp.IServices;
using Microsoft.AspNetCore.Authorization;
using System.Collections;
using System.Linq;
using DjErp.Repositories;
using VOL.Core.DBManager;

namespace DjErp.Controllers
{
    public partial class dj_stpoController
    {
        private readonly Idj_stpoService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public dj_stpoController(
            Idj_stpoService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 获取BOM的子节点
        /// </summary>
        /// <param name="f_id">父物料号</param>
        /// <returns></returns>
        [HttpPost, Route("get_bom_children"), AllowAnonymous]
        public dynamic get_bom_children(string f_id)
        {
            Hashtable rtn_hst = new Hashtable();
            //IQueryable<dj_stpo> dj_stpo_queryeable = dj_stpoRepository.Instance.FindAsIQueryable(t => (t.matnr == f_id));
            List<dynamic> dynamics = DBServerProvider.SqlDapper.QueryList<dynamic>("select *, (select count(*) from dj_stpo where matnr=dj_stpo_mara_view.idnrk) child_num from dj_stpo_mara_view where matnr=@matnr_id", new { matnr_id = f_id }).ToList();
            rtn_hst.Add("dj_stpo_children", dynamics);
            // Console.WriteLine(dj_stpo_queryeable.Count());
            return rtn_hst;
        }
        /// <summary>
        /// 获取BOM的所有子节点
        /// </summary>
        /// <param name="f_id">当前物料号</param>
        /// <returns></returns>
        [HttpPost, Route("get_bom_all_children"), AllowAnonymous]
        public dynamic get_bom_all_children(string f_id)
        {
            Hashtable rtn_hst = new Hashtable();
            List<dj_stpo_mara_view> dj_stpo_mara_view_ls = dj_stpo_mara_viewRepository.Instance.FindAsIQueryable(t => (1 == 1)).ToList();
            List<string> all_children = new List<string>();
            all_children = get_all_bom_childer(dj_stpo_mara_view_ls, f_id, all_children);
            all_children.Add(f_id);//为了查询树形根的物料中文
            return dj_stpo_mara_view_ls.Where(w => all_children.Contains(w.idnrk)).Select(s => new
            {
                Id = s.idnrk,
                ParentId = s.matnr,
                label = s.idnrk,
                maktx = s.maktx,
            });

        }



        /// <summary>
        /// 递归获取所有的BOM子件
        /// </summary>
        /// <param name="dj_stpo_all">所有的BOM数据</param>
        /// <param name="f_id">父件</param>
        /// <param name="return_all_children">获取的BOM子件清单，作为参数，用于每次递归时不断累加</param>
        /// <returns></returns>
        private List<string> get_all_bom_childer(List<dj_stpo_mara_view> dj_stpo_all, string f_id, List<string> return_all_children)
        {
            List<dj_stpo_mara_view> child = dj_stpo_all.Where(w => w.matnr == f_id).ToList();
            if (child.Count > 0)
            {
                foreach (var item in child)
                {
                    return_all_children.Add(item.idnrk);
                    get_all_bom_childer(dj_stpo_all, item.idnrk, return_all_children);
                }
            }

            return return_all_children;

        }
        /// <summary>
        /// 获取所有整车BOM的头物料号（查询有多少款BOM树）
        /// 逻辑为：查询没有子物料的父物料号
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("get_fert"), AllowAnonymous]
        public dynamic get_fret()
        {
            Hashtable rtn_hst = new Hashtable();
            List<dynamic> dynamics = DBServerProvider.SqlDapper.QueryList<dynamic>("select distinct matnr,maktx_parent from dj_stpo_mara_view where matnr not in(select idnrk from dj_stpo)", new { }).ToList();

            rtn_hst.Add("dj_maras", dynamics);

            return rtn_hst;
        }
    }
}
